// server/api/ai/chat.post.ts
export default defineEventHandler(async (event) => {
    const body = await readBody(event)
    const { messages, model = 'qwen-plus', stream = false } = body
    
    // 获取API Key（从环境变量）
    const config = useRuntimeConfig()
    const apiKey = config.aiApiKey || process.env.AI_API_KEY
    
    if (!apiKey) {
      throw createError({
        statusCode: 500,
        statusMessage: 'AI服务未配置'
      })
    }
    
    try {
      // 调用AI接口（以DeepSeek为例）[2,4](@ref)
      const response = await $fetch('https://api.deepseek.com/v1/chat/completions', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${apiKey}`
        },
        body: JSON.stringify({
          model: model,
          messages: messages,
          stream: stream,
          temperature: 0.7,
          max_tokens: 2000
        })
      })
      
      return {
        success: true,
        data: response
      }
    } catch (error) {
      console.error('AI API调用失败:', error)
      throw createError({
        statusCode: 500,
        statusMessage: 'AI服务暂时不可用，请稍后重试'
      })
    }
  })